package cn.wolfcode.trip.admin.controller;

import cn.wolfcode.trip.base.domain.Product;
import cn.wolfcode.trip.base.domain.ShopingCar;
import cn.wolfcode.trip.base.domain.Strategy;
import cn.wolfcode.trip.base.query.ProductQueryObject;
import cn.wolfcode.trip.base.service.IProductService;
import cn.wolfcode.trip.base.service.IShopingCarService;
import cn.wolfcode.trip.base.util.JSONResult;
import cn.wolfcode.trip.base.util.UploadUtil;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

/**
 * 结伴游接口
 */
@Controller
@RequestMapping("/product")
public class ProductController {
    @Autowired
    private IProductService productService;


    /**
     * 产品列表的高级查询
     *
     * @param qo
     * @return
     */
    @RequestMapping("/list")
    public String list(@ModelAttribute("qo") ProductQueryObject qo, Model model) {
        PageInfo pageInfo = productService.query(qo);
        model.addAttribute("pageInfo", pageInfo);
        return "product/list";
    }

    /**
     * 产品的新增或更改
     *
     * @param product
     * @param file
     * @return
     */
    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    public JSONResult saveOrUpdate(Product product, MultipartFile file) {
        if (file != null && file.getSize() > 0) {
            String url = UploadUtil.uploadQiniu(file);
            product.setImgUrls(url);
        }
        productService.saveOrUpdate(product);
        return new JSONResult();
    }

    /**
     *  产品的删除
     * @param id
     * @return
     */
    @RequestMapping("/deleteById")
    @ResponseBody
    public JSONResult deleteById(Long id) {
        if (id != null) {
            productService.deleteById(id);
        }
        return new JSONResult();
    }

}
